<?php
header('content-type:text/html; charset=utf-8');
require_once('config.php');
//打开数据库连接
$conn = new mysqli($host,$user='root',$password='root',$database);
if($conn->connect_error){
    die("连接失败".$conn->connect_error);
}
$methods = $_POST['method'];
if($methods == 'login')
{
    $username = $_POST['username'];
    $password = $_POST['password'];
    //sql查询语句
    $sql="SELECT * FROM login where username='$username' and password='$password'";
    $arr=array();
    $res=$conn->query($sql);
    if($res->num_rows>0){
        while($row=$res->fetch_assoc()){
            array_push($arr,$row);
        }
        echo json_encode(array('code'=>200,'data'=>$arr));
    }
    else {
        echo json_encode(array('code'=>300,'data'=>0));
    }
}

if($methods == "register"){
    $nickname = $_POST['nickname'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $idnumber = 0;
    $money = 0;
    $mumber = 0;
    $name = null;
    //sql查询语句
    $sql1="SELECT * FROM login where username='$username'";
    $res=$conn->query($sql1);
    if($res->num_rows>0){
        echo json_encode(array('code'=>100,'data'=>2));
    }else{
        $sql="INSERT INTO login (nickname , username , password , idnumber , money , mumber , name) values('$nickname','$username','$password','$idnumber','$money','$mumber','$name')";
        echo json_encode(array('code'=>$conn->query($sql)?200:300,'data'=>2));
    }
}

if($methods == "inf"){
    $username = $_POST['username'];
    //sql查询语句
    $sql1="SELECT * FROM login where username='$username'";
    $arr=array();
    $res=$conn->query($sql1);
    if($res->num_rows>0){
        while($row=$res->fetch_assoc()){
            array_push($arr,$row);
        }
        echo json_encode(array('code'=>200,'data'=>$arr));
    }else{
        echo json_encode(array('code'=>300,'data'=>2));
    }
}
if($methods == "ujoin"){
    $username = $_POST['username'];
    $name = $_POST['name'];
    $idnumber = $_POST['idnumber'];
    //sql查询语句
    $sql1="UPDATE login SET name='$name' , idnumber='$idnumber' , mumber='1' where username = '$username'";
    $arr=array();
    $res=$conn->query($sql1);
    echo json_encode(array('code'=>200,'data'=>1));
}
if($methods == "umoney"){
    $username = $_POST['username'];
    $money = $_POST['umoney'];
    //sql查询语句
    $sql1="UPDATE login SET money=money + $money where username = '$username'";
    $arr=array();
    $res=$conn->query($sql1);
    echo json_encode(array('code'=>200,'data'=>1));
}
//echo json_encode(array('code'=>$conn->query($sql)?200:300,'data'=>1));//输出JSON array('code'=>$conn->query($sql)?200:300,'data'=>$conn->insert_id)   INSERT INTO login (username , password) values($username,$password)
?>